package com.tyae.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.tyae.bean.Advogado;
import com.tyae.bean.Agenda;
import com.tyae.bean.Empresa;


public class AgendaDAOHibernate implements AgendaDAO {

	HibernateDAOFactory hibernateDAOFactory = null;
	HibernateDAO<Agenda> hibernateDAO = null;

	public AgendaDAOHibernate() {
		hibernateDAOFactory = new HibernateDAOFactory();
		hibernateDAO = hibernateDAOFactory.getAgendaDAO();
	}
	
	@Override
	public boolean inserir(Agenda agenda) throws Exception {
		boolean ok = false;

		try {
			hibernateDAO.add(agenda);
			ok = true;
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} 
		return ok;
	}

	@Override
	public boolean alterar(Agenda agenda) throws Exception {
		boolean ok = false;

		try {
			hibernateDAO.update(agenda);
			ok = true;
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}

		return ok;
	}

	@Override
	public boolean excluir(Agenda agenda) throws Exception {
		boolean ok = false;

		try {
			hibernateDAO.remove(agenda);
			ok = true;
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} 

		return ok;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Agenda> getSelecionar(String descricao) {
	    
		Criteria criteria = hibernateDAO.getSession().createCriteria(Empresa.class);
	    criteria.add(Restrictions.like("agenda.advogado.pessoa.nome",descricao,MatchMode.ANYWHERE));
	    List<Agenda> lista = criteria.list();	
 
		
		return lista;
	}	

	@Override
	public Agenda getAgenda(int codigoAgenda) {
		return hibernateDAO.get(codigoAgenda);
	}

	@Override
	public List<Agenda> getListarTodos() {
		return hibernateDAO.listAll();
	}
	
	@Override
	public boolean isExiste(Agenda agenda) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isExclusaoLiberada(Agenda agenda) {
		// TODO Auto-generated method stub
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Agenda> getSelecionar(String descricao, int empresa) {
		
		/*
		Criteria criteria = hibernateDAO.getSession().createCriteria(Agenda.class);
	    criteria.add(Restrictions.like("descricao",descricao,MatchMode.ANYWHERE));
	    criteria.add(Restrictions.eq("codigoEmpresa", empresa));
	    List<Agenda> lista = criteria.list();	
 */
		
		String 	sql = "FROM Agenda as agenda WHERE agenda.codigoAgenda <> 0";

		if (!(descricao.isEmpty())) {
			sql += " AND agenda.advogado.pessoa.nome like '" + descricao + "%' ";  
		}
		
		sql += " AND agenda.empresa = "+empresa;
		sql += " ORDER BY agenda.advogado.pessoa.nome";		
 
		List<Agenda> lista = hibernateDAO.getSession().createQuery(sql).list();
		
		return lista;	
	}

	@Override
	public Agenda getSelecionar(String nomeAdvogao, int empresa, Date data, String texto) {
		String 	sql = "SELECT DISTINCT agenda FROM Agenda as agenda WHERE agenda.codigoAgenda <> 0";

		if (!(nomeAdvogao.isEmpty())) {
			sql += " AND agenda.advogado.pessoa.nome = '" + nomeAdvogao + "' ";  
		}
		
		if (!(nomeAdvogao.isEmpty())) {
			sql += " AND agenda.observacao = '" + texto + "' ";  
		}
		if(data != null && !data.equals("")){
			sql +=		" AND agenda.data >= '" +new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(data)+ "'";
			sql +=		" AND agenda.data <= '" +new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(data)+ "'";
		}
		
		
		sql += " AND agenda.empresa = "+empresa;
		sql += " ORDER BY agenda.advogado.pessoa.nome";		
 
		return (Agenda) hibernateDAO.getSession().createQuery(sql).uniqueResult();	
	}

	@SuppressWarnings("unchecked")
	@Override
	public void excluir(Advogado advogado, Date data) throws Exception {
		String 	sql = "SELECT DISTINCT agenda FROM Agenda as agenda WHERE agenda.codigoAgenda <> 0";

		sql += " AND agenda.advogado.codigoAdvogado = '" + advogado.getCodigoAdvogado() + "' ";  
		
		sql +=		" AND agenda.data >= '" +new SimpleDateFormat("yyyy-MM-dd").format(data)+ " 00:00:00'";
		sql +=		" AND agenda.data <= '" +new SimpleDateFormat("yyyy-MM-dd").format(data)+ " 23:59:59'";
		
		List<Agenda> lista = hibernateDAO.getSession().createQuery(sql).list();
		for (Agenda agenda : lista) {
			excluir(agenda);
		}
	}	

}
